package com.carkeeper.evaluate.controller;

import com.carkeeper.common.utils.PageResult;
import com.carkeeper.common.utils.Result;
import com.carkeeper.common.utils.StatusCode;
import com.carkeeper.evaluate.pojo.Evaluate;
import com.carkeeper.evaluate.service.EvaluateService;
import com.github.pagehelper.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author zhangmingxin
 * @version 1.0
 * @date 2020/11/23 18:57
 */
@RestController
@RequestMapping("/evaluate")
@Api(tags = "评价服务接口api")//标签专门用来标注类的
public class EvaluateController {

    @Autowired
    private EvaluateService evaluateService;

    /**
     * 查询列表
     * @return
     */
    @GetMapping("/findAll")
    @ApiOperation("查询列表接口")//标注方法
    public Result<List<Evaluate>> findAll() {
        List<Evaluate> evaluateList = evaluateService.findAll();
        System.out.println("evaluateList = " + evaluateList);
        return new Result<>(true, StatusCode.OK, "查询列表成功", evaluateList);
    }

    /**
     * 根据id查询评价
     * @param id
     * @return
     */
    @GetMapping("/findById/{oid}")
    @ApiOperation("根据id查询评价接口")//标注方法
    @ApiImplicitParams({//参数集合
            @ApiImplicitParam(name = "id",value = "主键",defaultValue = "290682")
    })
    public Result<Evaluate> findById(@PathVariable("oid")Integer id) {
      Evaluate evaluate = evaluateService.findById(id);
      return new Result<>(true, StatusCode.OK, "查询评价成功", evaluate);
    }

    /**
     * 根据id删除单条记录
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    @ApiOperation("根据id删除单条记录接口")//标注方法
    public Result deleteById(@PathVariable("id")String id) {
        try {
            Integer count = evaluateService.deleteById(id);
            if (count>0){
                return new Result<>(true, StatusCode.OK, "删除成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result<>(true, StatusCode.ERROR, "删除失败");
    }

    /**
     * 多条件搜索查询
     * @return
     */
    @ApiOperation("多条件搜索查询接口")//标注方法
    @GetMapping("/findPage/{pageNum}/{pageSize}")
    public Result searchMap(@RequestParam Map searchMap, @PathVariable("pageSize")Integer pageSize, @PathVariable("pageNum")Integer pageNum){
        Page<Evaluate> pageList =  evaluateService.searchMap(searchMap,pageSize,pageNum);
        PageResult pageResult = new PageResult<>(pageList.getTotal(),pageList.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }
}
